{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1e81d96",
   "metadata": {},
   "source": [
    "-------\n",
    "\n",
    "* 本周主要内容：语音识别\n",
    "* 22春_API_人工智能与机器学习_week12\n",
    "*  电子讲义设计者：许智超\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25979f0",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 关于麦克风包安装\n",
    "\n",
    "## 优化ASR(自动语音识别)机器人\n",
    "\n",
    "## 连接图灵机器人(不用自己的字典)\n",
    "\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d2b2d61",
   "metadata": {},
   "source": [
    "# 关于麦克风包安装"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbe67cf8",
   "metadata": {},
   "source": [
    "* 安装 speech_recognition\n",
    "> pip install SpeechRecognition\n",
    "\n",
    "* 安装 PyAudio\n",
    "> Not found name \"PyAudio\" moudle\n",
    "\n",
    "> pip install pipwin\n",
    "\n",
    "> pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7aba7a",
   "metadata": {},
   "source": [
    "# 优化ASR(自动语音识别)机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cbd1bce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 智超 ：百度密钥\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eccab383",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aed5e1b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.5d585cd1f589871e15f50bfb9dc1b456.2592000.1655346818.282335-19331335 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.5d585cd1f589871e15f50bfb9dc1b456.2592000.1655346818.282335-19331335'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f9f96919",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "41789939",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.333568\n",
      "{\"corpus_no\":\"7098527910216000134\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"567055479741652754822\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7098527910216000134\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"567055479741652754822\"}\\n'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(xu_token,'1.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e07fa9f3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
